﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing

Namespace Controls

    <DefaultProperty("Location"), _
    ToolboxData("<{0}:Forecast runat=server></{0}:Forecast>"), _
    ToolboxBitmap(GetType(ResourceFinder), "Forecast.png")> _
    Public Class Forecast
        Inherits WebControl

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property Location() As String
            Get
                Dim s As String = CStr(ViewState("Location"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("Location") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property PartnerID() As String
            Get
                Dim s As String = CStr(ViewState("PartnerID"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("PartnerID") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property Key() As String
            Get
                Dim s As String = CStr(ViewState("Key"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("Key") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property Units() As Weather.Data.Units
            Get
                Dim s As String = CStr(ViewState("Units"))
                If s Is Nothing Then
                    Return Weather.Data.Units.Standard
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As Weather.Data.Units)
                ViewState("Units") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property Length() As Int16
            Get
                Dim s As String = CStr(ViewState("Length"))
                If s Is Nothing Then
                    Return 3
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As Int16)
                ViewState("Length") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property IconFolder() As String
            Get
                Dim s As String = CStr(ViewState("IconFolder"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("IconFolder") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property WeatherIconType() As Weather.Data.IconTypes
            Get
                Dim s As String = CStr(ViewState("WeatherIconType"))
                If s Is Nothing Then
                    Return Weather.Data.IconTypes.Png
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As Weather.Data.IconTypes)
                ViewState("WeatherIconType") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property ShowDetails() As Boolean
            Get
                If IsNothing(ViewState("ShowDetails")) Then
                    Return True
                Else
                    Return CBool(ViewState("ShowDetails"))
                End If
            End Get
            Set(ByVal Value As Boolean)
                ViewState("ShowDetails") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property ShowLegend() As Boolean
            Get
                If IsNothing(ViewState("ShowLegend")) Then
                    Return True
                Else
                    Return CBool(ViewState("ShowLegend"))
                End If
            End Get
            Set(ByVal Value As Boolean)
                ViewState("ShowLegend") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property ShowWeatherLogo() As Boolean
            Get
                If IsNothing(ViewState("ShowWeatherLogo")) Then
                    Return True
                Else
                    Return CBool(ViewState("ShowWeatherLogo"))
                End If
            End Get
            Set(ByVal Value As Boolean)
                ViewState("ShowWeatherLogo") = Value
            End Set
        End Property

        Public Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)
            AddAttributesToRender(writer)
            writer.RenderBeginTag(HtmlTextWriterTag.Div)
        End Sub

        Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
            With writer
                'Data.
                Dim o As Object = Weather.Data.Provider.GetForecast(Me.Location, Me.PartnerID, Me.Key, Me.Units, Me.Length)
                Dim fc As Weather.Data.Forecast = Nothing
                Dim tr As String = String.Empty

                'Check errors.
                If TypeOf o Is Weather.Data.ServiceError Then
                    Dim err As Weather.Data.ServiceError = DirectCast(o, Weather.Data.ServiceError)
                    .Write(String.Format("<div class=""weather-error"">"))
                    .Write(err.Description)
                    .Write(String.Format("</div>"))
                    Exit Sub
                Else
                    fc = DirectCast(o, Weather.Data.Forecast)
                End If

                'Icon folders.
                If Me.IconFolder.EndsWith("/") = False Then Me.IconFolder = String.Format("{0}/", Me.IconFolder)
                Dim if1 As String = String.Format("{0}Weather/", Me.IconFolder)
                Dim if2 As String = String.Format("{0}Moon/", Me.IconFolder)
                Dim if3 As String = String.Format("{0}Other/", Me.IconFolder)

                'Location.
                .Write(String.Format("<div class=""weather-location"">"))
                .Write(fc.Location)
                .Write(String.Format("</div>"))

                'Days.
                .Write(String.Format("<div class=""weather-days"">"))
                For Each d As Weather.Data.ForecastDay In fc.Days
                    If d Is fc.Days.Item(fc.Days.Count - 1) Then
                        .Write(String.Format("<div class=""weather-day weather-day-last"">"))
                    Else
                        .Write(String.Format("<div class=""weather-day"">"))
                    End If

                    'Title.
                    .Write(String.Format("<div class=""weather-day-title"">"))
                    .Write(My.Resources.Weather.ResourceManager.GetString(d.Name))
                    .Write(String.Format("</div>"))

                    'DAY.
                    .Write(String.Format("<div class=""weather-day-subtitle"">"))
                    .Write(My.Resources.Weather.Day)
                    .Write(String.Format("</div>"))
                    'Icon.
                    .Write(String.Format("<div class=""weather-icon"">"))
                    Select Case Me.WeatherIconType
                        Case Data.IconTypes.Png
                            .Write(String.Format("<img src=""{0}{1}.png"" alt=""{2}""/>", if1, d.DayIcon, d.DayDescription))
                        Case Data.IconTypes.Gif
                            .Write(String.Format("<img src=""{0}{1}.gif"" alt=""{2}""/>", if1, d.DayIcon, d.DayDescription))
                    End Select
                    .Write(String.Format("</div>"))

                    'Main content.
                    .Write(String.Format("<div class=""weather-main-content"">"))
                    .Write(String.Format("<div class=""weather-temperature"">{0}&deg;{1}</div>", d.MaxTemperature, fc.UnitTemperature))
                    .Write(String.Format("<div class=""weather-description"">{0}</div>", My.Resources.Weather.ResourceManager.GetString(d.DayDescription)))
                    .Write(String.Format("</div>"))

                    'Separator.
                    .Write(String.Format("<div class=""weather-separator"">"))
                    .Write(String.Format("</div>"))

                    'Details.
                    If Me.ShowDetails = True Then
                        tr = "<tr class=""{3}""><td class=""weather-detail-label"">{0}:</td><td class=""weather-detail-data"">{1}{2}</td></tr>"
                        .Write(String.Format("<div class=""weather-detail-content"">"))
                        .Write(String.Format("<table cellpadding=""0"" cellspacing=""0"" width=""100%"">"))
                        .Write(String.Format(tr, My.Resources.Weather.WindSpeed, d.DayWindSpeed, String.Format(" {0}", fc.UnitSpeed), "weather-detail-data-row"))
                        .Write(String.Format(tr, My.Resources.Weather.WindDirection, String.Format("{0}&deg;", d.DayWindDirection), String.Format(" {0}", Data.GetDirectionResource(d.DayWindDirectionDescription)), "weather-detail-data-altrow"))
                        .Write(String.Format(tr, My.Resources.Weather.RelativeHumidity, d.DayRelativeHumidity, "&#37;", "weather-detail-data-row"))
                        .Write(String.Format(tr, My.Resources.Weather.Precipitation, d.DayPrecipitation, "&#37;", "weather-detail-data-altrow"))
                        .Write(String.Format(tr, My.Resources.Weather.Sunrise, FormatDateTime(d.Sunrise, DateFormat.ShortTime), String.Empty, "weather-detail-data-row"))
                        .Write(String.Format("</table>"))
                        .Write(String.Format("</div>"))
                    End If

                    'NIGHT.
                    .Write(String.Format("<div class=""weather-day-subtitle"">"))
                    .Write(My.Resources.Weather.Night)
                    .Write(String.Format("</div>"))
                    'Icon.
                    .Write(String.Format("<div class=""weather-icon"">"))
                    Select Case Me.WeatherIconType
                        Case Data.IconTypes.Png
                            .Write(String.Format("<img src=""{0}{1}.png"" alt=""{2}""/>", if1, d.NightIcon, d.NightDescription))
                        Case Data.IconTypes.Gif
                            .Write(String.Format("<img src=""{0}{1}.gif"" alt=""{2}""/>", if1, d.NightIcon, d.NightDescription))
                    End Select
                    .Write(String.Format("</div>"))

                    'Main content.
                    .Write(String.Format("<div class=""weather-main-content"">"))
                    .Write(String.Format("<div class=""weather-temperature"">{0}&deg;{1}</div>", d.MinTemperature, fc.UnitTemperature))
                    .Write(String.Format("<div class=""weather-description"">{0}</div>", My.Resources.Weather.ResourceManager.GetString(d.NightDescription)))
                    .Write(String.Format("</div>"))

                    'Separator.
                    .Write(String.Format("<div class=""weather-separator"">"))
                    .Write(String.Format("</div>"))

                    'Details.
                    If Me.ShowDetails = True Then
                        .Write(String.Format("<div class=""weather-detail-content"">"))
                        .Write(String.Format("<table cellpadding=""0"" cellspacing=""0"" width=""100%"">"))
                        .Write(String.Format(tr, My.Resources.Weather.WindSpeed, d.NightWindSpeed, String.Format(" {0}", fc.UnitSpeed), "weather-detail-data-row"))
                        .Write(String.Format(tr, My.Resources.Weather.WindDirection, String.Format("{0}&deg;", d.NightWindDirection), String.Format(" {0}", Data.GetDirectionResource(d.NightWindDirectionDescription)), "weather-detail-data-altrow"))
                        .Write(String.Format(tr, My.Resources.Weather.RelativeHumidity, d.NightRelativeHumidity, "&#37;", "weather-detail-data-row"))
                        .Write(String.Format(tr, My.Resources.Weather.Precipitation, d.NightPrecipitation, "&#37;", "weather-detail-data-altrow"))
                        .Write(String.Format(tr, My.Resources.Weather.Sunset, FormatDateTime(d.Sunset, DateFormat.ShortTime), String.Empty, "weather-detail-data-row"))
                        .Write(String.Format("</table>"))
                        .Write(String.Format("</div>"))
                    End If

                    .Write(String.Format("</div>"))
                Next

                'Separator.
                .Write(String.Format("<div class=""weather-separator"">"))
                .Write(String.Format("</div>"))
                .Write(String.Format("</div>"))

                'Legend.
                If Me.ShowLegend = True Then
                    .Write(String.Format("<div class=""weather-forecast-legend"">"))
                    .Write(String.Format("<table cellpadding=""0"" cellspacing=""0"" width=""100%""><tr>"))
                    .Write(String.Format("<td class=""weather-forecast-legend-icon""><img src=""{0}08.png"" alt=""{1}""/></td>", if3, My.Resources.Weather.Temperature))
                    .Write(String.Format("<td class=""weather-forecast-legend-data"">{0}</td>", My.Resources.Weather.ForecastLegend))
                    .Write(String.Format("</tr></table>"))
                    .Write(String.Format("</div>"))
                End If

                'Weather Channel.
                If Me.ShowWeatherLogo = True Then
                    .Write(String.Format("<div class=""weather-channel"">"))
                    .Write(String.Format("<a href=""http://weather.com"" target=""_blank""><img src=""{0}"" alt=""{1}"" style=""border:none;""/></a>", Me.Page.ClientScript.GetWebResourceUrl(GetType(Controls.CurrentConditions), "Manic.Services.Weather.WeatherChannel.png"), My.Resources.Weather.TheWeatherChannel))
                    .Write(String.Format("</div>"))
                End If

                'Separator.
                .Write(String.Format("<div class=""weather-separator"">"))
                .Write(String.Format("</div>"))

                'Update time.
                .Write(String.Format("<div class=""weather-updatetime"">"))
                .Write(Format(fc.LastUpdate, "f"))
                .Write(String.Format("</div>"))
            End With
        End Sub

        Public Overrides Sub RenderEndTag(ByVal writer As System.Web.UI.HtmlTextWriter)
            writer.RenderEndTag()
        End Sub

    End Class

End Namespace